package com.winhxd.b2c.customer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.customer.condition.QueryPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerPageConfig;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.customer.dao.CustomerPageConfigMapper;
import com.winhxd.b2c.customer.service.PageConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: hxd-outlet-parent
 * @description: 页面配置service实现类
 * @author: ChenYanqi
 * @create: 2019-01-11 19:32
 **/
@Service
public class PageConfigServiceImpl implements PageConfigService {

    private Logger logger = LoggerFactory.getLogger(PageConfigServiceImpl.class);

    @Autowired
    CustomerPageConfigMapper customerPageConfigMapper;


    @Override
    public Boolean savePageConfig(CustomerPageConfig customerPageConfig) {
        validatePageTitle(customerPageConfig);
        int i = customerPageConfigMapper.insert(customerPageConfig);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public Boolean modifyPageConfig(CustomerPageConfig customerPageConfig) {
        validatePageTitle(customerPageConfig);
        int i = customerPageConfigMapper.updateByPrimaryKeySelective(customerPageConfig);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public Boolean releasePageConfig(CustomerPageConfig customerPageConfig) {
        int i = customerPageConfigMapper.updateByPrimaryKeySelective(customerPageConfig);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public Boolean deletePageConfig(Long id) {
        int i = customerPageConfigMapper.deleteByPrimaryKey(id);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public PagedList<CustomerPageConfig> selectByStoreId(QueryPageConfigCondition condition, Boolean homePage) {
        PagedList<CustomerPageConfig> pagedList = new PagedList<>();
        int pageNo = condition.getPageNo();
        int pageSize = condition.getPageSize();
        PageHelper.startPage(pageNo, pageSize);
        List<CustomerPageConfig> customerPageConfigs = null;
        if (homePage) {
            customerPageConfigs = customerPageConfigMapper.selectStorePageList(condition);
        } else {
            customerPageConfigs = customerPageConfigMapper.selectPageList(condition);
        }
        PageInfo pageInfo = new PageInfo(customerPageConfigs);
        pagedList.setData(customerPageConfigs);
        pagedList.setPageNo(pageNo);
        pagedList.setPageSize(pageSize);
        pagedList.setTotalRows(pageInfo.getTotal());
        return pagedList;
    }

    @Override
    public Boolean setHomePage(CustomerPageConfig customerPageConfig) {
        customerPageConfigMapper.updateHomePage(customerPageConfig);
        int i = customerPageConfigMapper.updateByPrimaryKeySelective(customerPageConfig);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public CustomerPageConfig selectPageConfigById(Long id) {
        CustomerPageConfig customerPageConfig = customerPageConfigMapper.selectByPrimaryKey(id);
        return customerPageConfig;
    }

    @Override
    public Boolean updateHomePage(CustomerPageConfig customerPageConfig) {
        int i = customerPageConfigMapper.updateHomePage(customerPageConfig);
        Boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public CustomerPageConfig selectHomePageByStoreId(Long storeId) {
        CustomerPageConfig customerPageConfig = customerPageConfigMapper.selectHomePageByStoreId(storeId);
        return customerPageConfig;
    }


    private void validatePageTitle(CustomerPageConfig customerPageConfig) {
        Long storeId = customerPageConfig.getStoreId();
        QueryPageConfigCondition condition = new QueryPageConfigCondition();
        condition.setStoreId(storeId);
        List<CustomerPageConfig> customerPageConfigs = customerPageConfigMapper.selectPageList(condition);
        if (StringUtils.isEmpty(customerPageConfig.getPageTitle())) {
            logger.info("新增或修改配置页面时，页面标题为空,storeId -{}", storeId);
            throw new BusinessException(BusinessCode.CODE_108002);
        }
        for (CustomerPageConfig pageConfig : customerPageConfigs) {
            if (customerPageConfig.getId() != null && pageConfig.getId().equals(customerPageConfig.getId())) {
                continue;
            }
            if (pageConfig.getPageTitle().equals(customerPageConfig.getPageTitle())) {
                logger.info("新增或修改配置页面时，页面标题重复,storeId -{}", storeId);
                throw new BusinessException(BusinessCode.CODE_108001);
            }
        }
    }


}
